package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.pojo.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工管理")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();
        return Result.success(employeeLoginVO);
    }

    /**
     * 添加员工
     *
     * @param dto 前端传递的员工部分参数
     * @return
     */
    @PostMapping
    @ApiOperation("新增员工")
    public Result<String> add(@RequestBody EmployeeDTO dto) {
        log.warn("添加的员工参数为：{}", dto);
//        Employee employee = employeeService.add(dto);

//        return employee != null ? Result.success("添加成功") : Result.error("添加失败");
        return employeeService.add(dto);
    }

    /**
     * 分页查询
     *
     * @param empPageDto
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> page(EmployeePageQueryDTO empPageDto) {
        log.warn("员工分页查询的参数为：{}", empPageDto);
        PageResult pr = employeeService.pageQuery(empPageDto);
        return Result.success(pr);
    }

    /**
     * 是否启用员工
     *
     * @param status 状态码
     * @param id 员工id
     * @return Result<String>
     */
    @PostMapping("/status/{status}")
    @ApiOperation("是否启用员工账号--状态")
    public Result<String> isEnable(@PathVariable Integer status, Long id) {
        log.warn("是否启用{}id为{}员工", status, id);
        employeeService.isEnable(status, id);
        return Result.success();
    }

    /** 根据id查询回显员工数据
     * @param id
     * @return Result<Employee>
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询回显员工数据")
    public Result<Employee> findById(@PathVariable Integer id) {
        return (employeeService.findById(id));
    }

    /** 修改员工信息
     * @param employee 修改的员工信息
     * @return
     */
    @PutMapping
    @ApiOperation("修改员工信息")
    public Result<String> update(@RequestBody Employee employee){
        employeeService.update(employee);
        return Result.success();
    }








    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出")
    public Result<String> logout() {
        return Result.success();
    }


}
